package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysAiChat;
import com.ruoyi.system.service.ISysAiChatService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * AI智能问答记录Controller
 * 
 * @author sxy
 * @date 2025-04-16
 */
@RestController
@RequestMapping("/chat")
public class SysAiChatController extends BaseController
{
    @Autowired
    private ISysAiChatService sysAiChatService;


    /**
     * 发起提问
     */
    @GetMapping("request")
    public AjaxResult request(String request) throws NoApiKeyException, InputRequiredException {
        return success(sysAiChatService.getAIResponse(request));
    }

    /**
     * 查询AI智能问答记录列表
     */
    @RequiresPermissions("system:chat:list")
    @GetMapping("/list")
    public TableDataInfo list(SysAiChat sysAiChat)
    {
        startPage();
        List<SysAiChat> list = sysAiChatService.selectSysAiChatList(sysAiChat);
        return getDataTable(list);
    }


    /**
     * 获取AI智能问答记录详细信息
     */
    @RequiresPermissions("system:chat:query")
    @GetMapping(value = "/{chatId}")
    public AjaxResult getInfo(@PathVariable("chatId") Long chatId)
    {
        return success(sysAiChatService.selectSysAiChatByChatId(chatId));
    }


    /**
     * 删除AI智能问答记录
     */
    @RequiresPermissions("system:chat:remove")
    @Log(title = "AI智能问答记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{chatIds}")
    public AjaxResult remove(@PathVariable Long[] chatIds)
    {
        return toAjax(sysAiChatService.deleteSysAiChatByChatIds(chatIds));
    }
}
